package com.znufe.exam.utility.easyexcel;

import com.alibaba.excel.EasyExcelFactory;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.VerticalAlignment;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

/**
 * @author zhy
 * @title: EasyExcelUtils
 * @projectName cec-moutai-bd-display
 * @description: easyExcel工具类
 * @date 2019/12/2411:35
 */
public class EasyExcelUtils {

    private EasyExcelUtils(){}


    @SuppressWarnings("rawtypes")
	public static void webWriteExcel(HttpServletResponse response, List objects, Class clazz, String fileName) throws IOException {
        String sheetName = fileName;    //  new String(fileName.getBytes("ISO-8859-1"),"utf-8")
        webWriteExcel(response,objects,clazz,fileName,sheetName);
    }

    
    @SuppressWarnings("rawtypes")
	public static void webWriteExcel(HttpServletResponse response, List objects, Class clazz, String fileName, String sheetName) throws IOException {
        response.setContentType("application/vnd.ms-excel; charset=utf-8");  // 原来括号里的内容 →  application/json
        response.setCharacterEncoding("utf-8");
        String file = new String(fileName.getBytes("ISO-8859-1"),"UTF-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(file, "UTF-8") + ".xlsx");
        // 头的策略
        WriteCellStyle headWriteCellStyle = new WriteCellStyle();
        // 背景设置为白
        headWriteCellStyle.setFillForegroundColor(IndexedColors.GOLD.getIndex());
        // 内容的策略
        WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
        HorizontalCellStyleStrategy horizontalCellStyleStrategy =
                new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);
        ServletOutputStream outputStream = response.getOutputStream();
        try {
            EasyExcelFactory.write(outputStream, clazz)
                    .registerWriteHandler(horizontalCellStyleStrategy)
                    .sheet(sheetName)
                    .doWrite(objects);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            outputStream.close();
        }
    }

    @SuppressWarnings("rawtypes")
    public static ServletOutputStream getFileOutPutStream(HttpServletResponse response,
                                                          String fileName) throws IOException {
        response.setContentType("application/vnd.ms-excel; charset=utf-8");  // 原来括号里的内容 →  application/json
        response.setCharacterEncoding("utf-8");
        String file = new String(fileName.getBytes("ISO-8859-1"),"UTF-8");
        response.setHeader("Content-Disposition", "attachment;filename=" +
                URLEncoder.encode(file, "UTF-8") + ".xlsx");
        return response.getOutputStream();
    }

    public static HorizontalCellStyleStrategy fileHeadStrategy() {
        // 头的策略
        WriteCellStyle headWriteCellStyle = new WriteCellStyle();
        // 背景设置为白
        headWriteCellStyle.setFillForegroundColor(IndexedColors.GOLD.getIndex());
        // 内容的策略
        WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
        // 设置单元格自动换行
        contentWriteCellStyle.setWrapped(true);
        // 设置单元格里的内容垂直居中
        contentWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        HorizontalCellStyleStrategy horizontalCellStyleStrategy =
                new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);
        return horizontalCellStyleStrategy;
    }

}
